package com.cnhis.cloudhealth.clinical.hosdoctor.medicalrecord.bo;

import com.cnhis.cloudhealth.clinical.hosdoctor.medicalrecord.dao.MedicalrecordDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by xiezhenyuan on 2016/3/24.
 */
@Service
public class MedicalrecordBo {
    @Resource
    private MedicalrecordDao medicalrecordDao;

    private List<Map<String,String>> changeToString(List<?> list){
        List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
        for(Object o:list){
            if(o instanceof Map){
                Map<String,Object> pmap = (Map<String,Object>)o;
                Map<String,String> smap = new HashMap<String,String>();
                Iterator<Map.Entry<String, Object>> it = pmap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> entry = it.next();
                    String str = String.valueOf(entry.getValue());
                    smap.put(entry.getKey(),str);
                }
                resultList.add(smap);
            }
        }
        return resultList;
    }

    private ModelVo packModelVo(String nid,Map map){
        ModelVo mo = new ModelVo();
        List<?> list = null;
        try {
            list = medicalrecordDao.queryForList(nid,map);
            list = changeToString(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(list!=null&&list.size()>0) {
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setTotal(list.size());
            mo.setRows(list);
        }
        return mo;
    }

    private ModelVo packModelVoPage(String nid,Map map,int pageno,int pagesize){
        ModelVo mo = new ModelVo();
        List<?> list = null;
        try {
            Page p = medicalrecordDao.queryForPage(nid,map,pageno,pagesize);
            list = changeToString(p.getList());
            if(list!=null&&list.size()>0) {
                mo.setList(list);
                mo.setResult(StaticKeys.OH_SUCCESS);
                mo.setTotal(p.getTotalNumber());
                mo.setRows(list);
                mo.setPage(pageno);
                mo.setRecords(p.getTotalNumber());
                mo.setTotalPage(p.getTotalPage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mo;
    }

    public ModelVo selectPmedrecord(Map map){return packModelVo("Medicalrecord.7129",map);}
    public ModelVo selectPbaby(Map map){return packModelVo("Medicalrecord.1169",map);}
    public ModelVo selectPusedrecord(Map map){
        try {
            List list = medicalrecordDao.queryForList("Medicalrecord.7128_001",map);
            System.out.println("查询List:"+list.size());
            if(list!=null&&list.size()>0){
               List<Integer> ll = new ArrayList<Integer>();
               for(Object o:list){
                   if(o instanceof Map){
                       Map<String,Object> cl = (Map<String,Object>)o;
                       Integer cade01 = (Integer)cl.get("cad01");
                       ll.add(cade01);
                   }
               }
               map.put("cadList",ll);
                //删除无效的病历
               List list2 =  medicalrecordDao.queryForList("Medicalrecord.7128_002",map);
                //删除无效的病历
               List list3 =  medicalrecordDao.queryForList("Medicalrecord.7128_003",map);
                //删除无效的科室病历
               List list4 =  medicalrecordDao.queryForList("Medicalrecord.7128_004",map);
               if(list2!=null&&list2.size()>0){
                   Iterator<Map<String,Object>> it = list.iterator();
                   while(it.hasNext()){
                       Map<String,Object> o = it.next();
                       Integer cade01 = (Integer)o.get("cad01");
                       Integer cad01a = (Integer)o.get("cad01a");
                       Integer cad08 = (Integer)o.get("cad08");
                       if(cade01!=null) {
                           Iterator<Map<String, Object>> it2 = list2.iterator();
                           while (it2.hasNext()) {
                               Map<String, Object> o2 = it2.next();
                               Integer cade02 = (Integer)o2.get("cad01");
                               if(cade02!=null&&cade02.intValue()==cade01.intValue()){
                                   it.remove();
                               }
                           }
                           Iterator<Map<String, Object>> it3 = list3.iterator();
                           boolean flag = false;
                           while (it3.hasNext()) {
                               Map<String, Object> o3 = it3.next();
                               Integer cade03 = (Integer)o3.get("cad01");
                               if(cade03!=null&&cade03.intValue()==cade01.intValue()){
                                   flag = true;
                               }
                           }
                           if(!flag&&cad01a!=null&&cad01a>0){
                               it.remove();
                               flag = false;
                           }

                           Iterator<Map<String, Object>> it4 = list4.iterator();
                           boolean flag2 = false;
                           while (it4.hasNext()) {
                               Map<String, Object> o4 = it4.next();
                               Integer cade04 = (Integer)o4.get("cad01");
                               if(cade04!=null&&cade04.intValue()==cade01.intValue()){
                                   flag2 = true;
                               }
                           }
                           if(!flag2&&cad08!=null&&cad08.intValue()==2){
                               it.remove();
                               flag2 = false;
                           }
                       }
                   }
               }
                System.out.println("过滤后List:"+list.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public ModelVo selectPsinglemedrecord(Map map){return packModelVo("Medicalrecord.7140",map);}
    public ModelVo selectPinandoutdate(Map map){return packModelVo("Medicalrecord.7163",map);}
    public ModelVo selectNursingrecord(Map map){return packModelVo("Medicalrecord.7155",map);}
    public ModelVo selectNtempraturerecord(Map map,int pageNo,int pageSize){return packModelVoPage("Medicalrecord.7170",map,pageNo,pageSize);}
    public ModelVo selectNoperationinfo(Map map,int pageNo,int pageSize){return packModelVoPage("Medicalrecord.7148",map,pageNo,pageSize);}
    public ModelVo selectMedicalrecordlist(Map map,int start,int end){return packModelVoPage("Medicalrecord.8007",map,start,end);}
    public ModelVo selectDischargeStatus(){return packModelVo("Medicalrecord.1362",null);}
    public ModelVo selectGradeClassify(){return packModelVo("Medicalrecord.806",null);}
    public ModelVo selectHealGrade(){return packModelVo("Medicalrecord.4197",null);}
    public ModelVo selectPatientMedrcd(Map map){return packModelVo("Medicalrecord.8029",map);}
    public ModelVo selectOperationRecord(Map map){return packModelVo("Medicalrecord.4195",map);}
    public ModelVo selectTransferRecord(Map map){return packModelVo("Medicalrecord.8025",map);}
    public ModelVo selectScheduleRecord(Map map){return packModelVo("Medicalrecord.8032",map);}
    public ModelVo selectAmountMoney(Map map){return packModelVo("Medicalrecord.8043",map);}
    public ModelVo selectFamilyRelationship(){return packModelVo("Medicalrecord.4197",null);}
    public ModelVo selectHospitalExpense(Map map){return packModelVo("Medicalrecord.8010",map);}
    public ModelVo selectValidCert(){return packModelVo("Medicalrecord.validcert",null);}
    public ModelVo selectOperationType(){return packModelVo("Medicalrecord.operationtype",null);}

}
